<template>
  <el-dialog
    v-model="visible"
    title="快捷键帮助"
    width="600px"
    :before-close="handleClose"
    class="shortcut-helper"
  >
    <div class="shortcut-content">
      <div class="shortcut-tabs">
        <div 
          v-for="category in categories" 
          :key="category.key"
          class="tab-item"
          :class="{ active: activeCategory === category.key }"
          @click="activeCategory = category.key"
        >
          <el-icon><component :is="category.icon" /></el-icon>
          <span>{{ category.label }}</span>
        </div>
      </div>
      
      <div class="shortcut-list">
        <div 
          v-for="shortcut in filteredShortcuts" 
          :key="shortcut.id"
          class="shortcut-item"
        >
          <div class="shortcut-keys">
            <kbd 
              v-for="key in shortcut.keys" 
              :key="key"
              class="key"
            >
              {{ key }}
            </kbd>
          </div>
          <div class="shortcut-description">
            <div class="shortcut-title">{{ shortcut.title }}</div>
            <div class="shortcut-desc">{{ shortcut.description }}</div>
          </div>
        </div>
      </div>
      
      <div class="shortcut-tips">
        <el-alert
          title="提示"
          type="info"
          :closable="false"
          show-icon
        >
          <template #default>
            <p>按 <kbd>?</kbd> 或 <kbd>Ctrl + /</kbd> 可以快速打开此帮助面板</p>
            <p>在不同页面下，可用的快捷键可能会有所不同</p>
          </template>
        </el-alert>
      </div>
    </div>
    
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleClose">关闭</el-button>
        <el-button type="primary" @click="printShortcuts">
          <el-icon><Printer /></el-icon>
          打印快捷键
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, computed, onMounted, onUnmounted } from 'vue'
import { 
  Monitor, Edit, Search, Setting, Printer
} from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'

// Props
const props = defineProps({
  modelValue: {
    type: Boolean,
    default: false
  }
})

// Emits
const emit = defineEmits(['update:modelValue'])

// 响应式数据
const activeCategory = ref('general')

// 计算属性
const visible = computed({
  get: () => props.modelValue,
  set: (value) => emit('update:modelValue', value)
})

// 快捷键分类
const categories = ref([
  { key: 'general', label: '通用', icon: 'Monitor' },
  { key: 'navigation', label: '导航', icon: 'Search' },
  { key: 'editing', label: '编辑', icon: 'Edit' },
  { key: 'settings', label: '设置', icon: 'Setting' }
])

// 快捷键列表
const shortcuts = ref([
  // 通用快捷键
  {
    id: 1,
    category: 'general',
    keys: ['Ctrl', '+', '/'],
    title: '显示快捷键帮助',
    description: '打开或关闭快捷键帮助面板'
  },
  {
    id: 2,
    category: 'general',
    keys: ['?'],
    title: '显示快捷键帮助',
    description: '快速打开快捷键帮助面板'
  },
  {
    id: 3,
    category: 'general',
    keys: ['Esc'],
    title: '关闭弹窗',
    description: '关闭当前打开的对话框或面板'
  },
  {
    id: 4,
    category: 'general',
    keys: ['Ctrl', '+', 'S'],
    title: '保存',
    description: '保存当前编辑的内容'
  },
  {
    id: 5,
    category: 'general',
    keys: ['Ctrl', '+', 'Z'],
    title: '撤销',
    description: '撤销上一步操作'
  },
  {
    id: 6,
    category: 'general',
    keys: ['Ctrl', '+', 'Y'],
    title: '重做',
    description: '重做上一步撤销的操作'
  },
  
  // 导航快捷键
  {
    id: 7,
    category: 'navigation',
    keys: ['Ctrl', '+', 'K'],
    title: '全局搜索',
    description: '打开全局搜索功能'
  },
  {
    id: 8,
    category: 'navigation',
    keys: ['Alt', '+', '←'],
    title: '后退',
    description: '返回上一页'
  },
  {
    id: 9,
    category: 'navigation',
    keys: ['Alt', '+', '→'],
    title: '前进',
    description: '前往下一页'
  },
  {
    id: 10,
    category: 'navigation',
    keys: ['Ctrl', '+', 'Home'],
    title: '回到首页',
    description: '快速返回首页'
  },
  {
    id: 11,
    category: 'navigation',
    keys: ['G', 'H'],
    title: '回到首页',
    description: '快速返回首页（连续按键）'
  },
  {
    id: 12,
    category: 'navigation',
    keys: ['G', 'C'],
    title: '进入课堂',
    description: '快速进入智能课堂'
  },
  {
    id: 13,
    category: 'navigation',
    keys: ['G', 'D'],
    title: '个人中心',
    description: '快速进入个人中心'
  },
  
  // 编辑快捷键
  {
    id: 14,
    category: 'editing',
    keys: ['Ctrl', '+', 'A'],
    title: '全选',
    description: '选择所有内容'
  },
  {
    id: 15,
    category: 'editing',
    keys: ['Ctrl', '+', 'C'],
    title: '复制',
    description: '复制选中的内容'
  },
  {
    id: 16,
    category: 'editing',
    keys: ['Ctrl', '+', 'V'],
    title: '粘贴',
    description: '粘贴剪贴板内容'
  },
  {
    id: 17,
    category: 'editing',
    keys: ['Ctrl', '+', 'X'],
    title: '剪切',
    description: '剪切选中的内容'
  },
  {
    id: 18,
    category: 'editing',
    keys: ['Ctrl', '+', 'F'],
    title: '查找',
    description: '在当前页面查找内容'
  },
  {
    id: 19,
    category: 'editing',
    keys: ['Ctrl', '+', 'H'],
    title: '替换',
    description: '查找并替换内容'
  },
  
  // 设置快捷键
  {
    id: 20,
    category: 'settings',
    keys: ['Ctrl', '+', ','],
    title: '打开设置',
    description: '打开系统设置面板'
  },
  {
    id: 21,
    category: 'settings',
    keys: ['Ctrl', '+', 'Shift', '+', 'T'],
    title: '切换主题',
    description: '在明亮和深色主题间切换'
  },
  {
    id: 22,
    category: 'settings',
    keys: ['Ctrl', '+', '+'],
    title: '放大',
    description: '放大页面内容'
  },
  {
    id: 23,
    category: 'settings',
    keys: ['Ctrl', '+', '-'],
    title: '缩小',
    description: '缩小页面内容'
  },
  {
    id: 24,
    category: 'settings',
    keys: ['Ctrl', '+', '0'],
    title: '重置缩放',
    description: '重置页面缩放到100%'
  },
  {
    id: 25,
    category: 'settings',
    keys: ['F11'],
    title: '全屏模式',
    description: '进入或退出全屏模式'
  }
])

// 计算属性
const filteredShortcuts = computed(() => {
  return shortcuts.value.filter(shortcut => shortcut.category === activeCategory.value)
})

// 方法
const handleClose = () => {
  visible.value = false
}

const printShortcuts = () => {
  const printContent = generatePrintContent()
  const printWindow = window.open('', '_blank')
  
  printWindow.document.write(`
    <html>
      <head>
        <title>ZenithMind 快捷键帮助</title>
        <style>
          body { font-family: Arial, sans-serif; margin: 20px; }
          h1 { color: #409eff; }
          h2 { color: #333; margin-top: 30px; }
          .shortcut-item { 
            display: flex; 
            justify-content: space-between; 
            margin: 10px 0; 
            padding: 8px;
            border-bottom: 1px solid #eee;
          }
          .shortcut-keys { font-weight: bold; }
          kbd { 
            background: #f5f5f5; 
            border: 1px solid #ccc; 
            border-radius: 3px; 
            padding: 2px 6px; 
            margin: 0 2px;
            font-size: 12px;
          }
          .shortcut-description { text-align: right; }
          .shortcut-title { font-weight: 500; }
          .shortcut-desc { font-size: 12px; color: #666; }
        </style>
      </head>
      <body>
        ${printContent}
      </body>
    </html>
  `)
  
  printWindow.document.close()
  printWindow.print()
  
  ElMessage.success('快捷键帮助已发送到打印机')
}

const generatePrintContent = () => {
  let content = '<h1>ZenithMind 快捷键帮助</h1>'
  
  categories.value.forEach(category => {
    const categoryShortcuts = shortcuts.value.filter(s => s.category === category.key)
    if (categoryShortcuts.length > 0) {
      content += `<h2>${category.label}</h2>`
      categoryShortcuts.forEach(shortcut => {
        const keys = shortcut.keys.map(key => `<kbd>${key}</kbd>`).join(' ')
        content += `
          <div class="shortcut-item">
            <div class="shortcut-keys">${keys}</div>
            <div class="shortcut-description">
              <div class="shortcut-title">${shortcut.title}</div>
              <div class="shortcut-desc">${shortcut.description}</div>
            </div>
          </div>
        `
      })
    }
  })
  
  return content
}

// 全局快捷键监听
const handleKeydown = (event) => {
  // Ctrl + / 或 ? 打开帮助
  if ((event.ctrlKey && event.key === '/') || event.key === '?') {
    event.preventDefault()
    visible.value = !visible.value
  }
  
  // Esc 关闭帮助
  if (event.key === 'Escape' && visible.value) {
    visible.value = false
  }
}

// 生命周期
onMounted(() => {
  document.addEventListener('keydown', handleKeydown)
})

onUnmounted(() => {
  document.removeEventListener('keydown', handleKeydown)
})
</script>

<style lang="scss" scoped>
.shortcut-helper {
  :deep(.el-dialog__body) {
    padding: 0;
  }
}

.shortcut-content {
  display: flex;
  flex-direction: column;
  height: 500px;
}

.shortcut-tabs {
  display: flex;
  border-bottom: 1px solid var(--el-border-color-lighter);
  background: var(--el-fill-color-extra-light);
}

.tab-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 20px;
  cursor: pointer;
  transition: all 0.3s ease;
  border-bottom: 2px solid transparent;
  
  &:hover {
    background: var(--el-fill-color-light);
  }
  
  &.active {
    background: white;
    border-bottom-color: var(--el-color-primary);
    color: var(--el-color-primary);
  }
  
  .el-icon {
    font-size: 16px;
  }
  
  span {
    font-size: 14px;
    font-weight: 500;
  }
}

.shortcut-list {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
}

.shortcut-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 0;
  border-bottom: 1px solid var(--el-border-color-extra-light);
  
  &:last-child {
    border-bottom: none;
  }
}

.shortcut-keys {
  display: flex;
  gap: 4px;
  align-items: center;
}

.key {
  background: var(--el-fill-color-light);
  border: 1px solid var(--el-border-color-lighter);
  border-radius: 4px;
  padding: 4px 8px;
  font-size: 12px;
  font-family: monospace;
  font-weight: 500;
  color: var(--el-text-color-primary);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
  min-width: 24px;
  text-align: center;
}

.shortcut-description {
  text-align: right;
  max-width: 60%;
}

.shortcut-title {
  font-weight: 500;
  color: var(--el-text-color-primary);
  margin-bottom: 2px;
}

.shortcut-desc {
  font-size: 12px;
  color: var(--el-text-color-secondary);
  line-height: 1.4;
}

.shortcut-tips {
  padding: 20px;
  border-top: 1px solid var(--el-border-color-lighter);
  
  :deep(.el-alert__content) {
    p {
      margin: 4px 0;
      
      kbd {
        background: var(--el-fill-color-light);
        border: 1px solid var(--el-border-color-lighter);
        border-radius: 3px;
        padding: 2px 6px;
        font-size: 11px;
        margin: 0 2px;
      }
    }
  }
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
}

@media (max-width: 768px) {
  .shortcut-content {
    height: 400px;
  }
  
  .shortcut-tabs {
    overflow-x: auto;
  }
  
  .tab-item {
    padding: 10px 16px;
    white-space: nowrap;
  }
  
  .shortcut-item {
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
  }
  
  .shortcut-description {
    text-align: left;
    max-width: 100%;
  }
}
</style>
